package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceDispatchOrderDetailAdapter;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.project.iview.IProductDetailView;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.presenter.ProductDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

public class ProduceDispatchOrderDetailActivity extends BaseActivity implements IProductDetailView, ItemClickListener {
    private String TAG = ProduceDispatchOrderDetailActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_product_name, tv_project_name, tv_engineering_name;
    private ImageView img_arrow_down;
    private LinearLayout ll_content;
    private TextView tv_type, tv_level, tv_size, tv_num, tv_start_date, tv_complete_date, tv_edit_person, tv_edit_time;
    private ListViewForScrollView work_part_lv;

    private Produce_ProductBean produce_productBean;

    private ProductDetailPresenter productDetailPresenter;

    private ProduceDispatchOrderDetailAdapter adapter;


    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_product_dispatch_order_detail);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_product_name = findViewById(R.id.tv_product_name);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        img_arrow_down = findViewById(R.id.img_arrow_down);
        ll_content = findViewById(R.id.ll_content);
        tv_type = findViewById(R.id.tv_type);
        tv_level = findViewById(R.id.tv_level);
        tv_size = findViewById(R.id.tv_size);
        tv_num = findViewById(R.id.tv_num);
        tv_start_date = findViewById(R.id.tv_start_date);
        tv_complete_date = findViewById(R.id.tv_complete_date);
        tv_edit_person = findViewById(R.id.tv_edit_person);
        tv_edit_time = findViewById(R.id.tv_edit_time);
        work_part_lv = findViewById(R.id.work_part_lv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.add_product_list));
        img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);

        Intent intent = getIntent();
        if (null != intent) {
            produce_productBean = (Produce_ProductBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        productDetailPresenter = new ProductDetailPresenter(this);
        productDetailPresenter.getProductDetail();

    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_arrow_down.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 收放
            case R.id.img_arrow_down:
                if (ll_content.getVisibility() == View.VISIBLE) {
                    ll_content.setVisibility(View.GONE);
                    img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);
                } else {
                    ll_content.setVisibility(View.VISIBLE);
                    img_arrow_down.setBackgroundResource(R.mipmap.arrow_top);
                }
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
        Intent intent;
        switch (view.getId()) {
            // 编辑工序派工信息
            case R.id.img_edit:
                if (null != produce_productBean) {
                    WorkPartBean wpbDate = produce_productBean.getWorkPartList().get(position);
                    intent = new Intent(ProduceDispatchOrderDetailActivity.this, ProduceEditDispatchInformationActivity.class);
                    intent.putExtra(ConstantUtil.KEY_1, CommonEnum.ROLECATEGORY.TEAM);
                    intent.putExtra(ConstantUtil.KEY_2, wpbDate);
                    startActivityForResult(intent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (null != data) {
                    WorkPartBean workPartBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                    for (int i = 0; i < produce_productBean.getWorkPartList().size(); i++) {
                        if (workPartBean.getWorkPartId().equals(produce_productBean.getWorkPartList().get(i).getWorkPartId())) {
                            produce_productBean.getWorkPartList().set(i, workPartBean);
                            adapter.setData(produce_productBean.getWorkPartList());
                            adapter.notifyDataSetChanged();
                            break;
                        }
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getOrderId() {
        return produce_productBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return produce_productBean.getProductId();
    }

    @Override
    public void getProductDetailSuccessed(Produce_ProductBean productBean) {
        produce_productBean = productBean;
        if (null != productBean.getWorkPartList() && !productBean.getWorkPartList().isEmpty()) {
            tv_product_name.setText(productBean.getProductName());
            tv_project_name.setText(" / " + productBean.getProjectName());
            tv_engineering_name.setText(" / " + productBean.getEngineeringName());
            if (null != productBean.getType()) {
                if ("标准工单".equals(productBean.getType())) {
                    tv_type.setText("A");
                } else if ("返工工单".equals(productBean.getType())) {
                    tv_type.setText("B");
                } else if ("报废工单".equals(productBean.getType())) {
                    tv_type.setText("C");
                } else if ("打样工单".equals(productBean.getType())) {
                    tv_type.setText("D");
                } else if ("免费工单".equals(productBean.getType())) {
                    tv_type.setText("E");
                }
            }
            CommonEnum.ProduceOrderLevel level = productBean.getLevel();
            if (null != level) {
                if (productBean.getLevel() == CommonEnum.ProduceOrderLevel.VERY_IMPORTANT) {
                    tv_level.setText(" / 一级");
                } else if (productBean.getLevel() == CommonEnum.ProduceOrderLevel.MORE_IMPORTANT) {
                    tv_level.setText(" / 二级");
                } else if (productBean.getLevel() == CommonEnum.ProduceOrderLevel.GENERALLY_IMPORTANT) {
                    tv_level.setText(" / 三级");
                } else if (productBean.getLevel() == CommonEnum.ProduceOrderLevel.NORMAL) {
                    tv_level.setText(" / 四级");
                }
            }

            CommonEnum.ProductShapeType shape = productBean.getShapeType().getType();
            if (null != shape) {
                if (shape == CommonEnum.ProductShapeType.ROUND) {
                    tv_size.setText("直径" + productBean.getShapeType().getDiameter() + "mm*" + "厚度" + productBean.getShapeType().getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                    tv_size.setText("长度" + productBean.getShapeType().getWidth() + "mm*" + "高度" + productBean.getShapeType().getHeight() + "mm*" + "厚度" + productBean.getShapeType().getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                    tv_size.setText(productBean.getShapeType().getDesc());
                }
            }

            tv_num.setText(productBean.getAmount() + productBean.getUnit());
            tv_start_date.setText(DateUtil.getYyyy_Mm_Dd(productBean.getBeginDate()));
            tv_complete_date.setText(DateUtil.getYyyy_Mm_Dd(productBean.getFinishDate()));
            tv_edit_person.setText(productBean.getEditAccountName() + "：" + productBean.getEditAccountId());
            tv_edit_time.setText(DateUtil.getYyyy_Mm_Dd(productBean.getEditDate()));

            adapter = new ProduceDispatchOrderDetailAdapter(this);
            adapter.setData(productBean.getWorkPartList());
            adapter.setItemClickListener(this);
            work_part_lv.setAdapter(adapter);
        }
    }

    @Override
    public void getProductDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
